use std::{fmt, string::FromUtf8Error};

use tracing::error;

pub struct FromBufError(&'static str);

impl std::error::Error for FromBufError {}

//实现Display
impl std::fmt::Display for FromBufError {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.fmt(f)
	}
}

//实现Display
impl std::fmt::Debug for FromBufError {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.fmt(f)
	}
}

impl FromBufError {
 	#[inline]
	pub fn new(str: &'static str) -> Self {
		Self(str)
	}

	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "从buf转换出现异常..{}", self.0)
	}
}

impl From<FromUtf8Error> for FromBufError {
	fn from(value: FromUtf8Error) -> Self {
		let s = std::backtrace::Backtrace::capture();
		error!("异常来源:{},,调用过程:{}", value, s);

		Self("不是utf-8格式")
	}
}

pub struct ToBufError(&'static str);

impl std::error::Error for ToBufError {}

//实现Display
impl std::fmt::Display for ToBufError {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.fmt(f)
	}
}

//实现Display
impl std::fmt::Debug for ToBufError {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.fmt(f)
	}
}

impl ToBufError {
	#[inline]
	pub fn new(str: &'static str) -> Self {
		Self(str)
	}

	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "写入buf出现异常..{}", self.0)
	}
}
